package web;

import pojo.SmbmsBill;
import pojo.SmbmsProvider;
import service.SmbmsBillService;
import service.SmbmsProviderService;
import service.impl.SmbmsBillServiceImpl;
import service.impl.SmbmsProviderServiceImpl;
import utils.Constants;
import utils.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/BillListServlet")
public class BillListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //调用业务层实现
        //账单表信息
        SmbmsBillService smbmsBillService=new SmbmsBillServiceImpl();
        //供应商表信息
        SmbmsProviderService smbmsProviderService =new SmbmsProviderServiceImpl();
        //获取账单表信息
        //获取商品名称
        String productname=req.getParameter("productname");
        System.out.println("productname     "+productname);
        //获取供应商名称（等待编写）
        String proId=req.getParameter("tigong");
        Integer providerId=Integer.parseInt(proId);

        //获取是否付款
        String fukuan=req.getParameter("fukuan");
        int ispayment=0;
        if(fukuan != null && !fukuan.equals("")){
            ispayment=Integer.parseInt(fukuan);
        }
        System.out.println(ispayment+"=========================");
        if(productname ==null){
            productname="";
        }
        //获取当前页
        String pageIndex=req.getParameter("pageIndex");
        int currentPage=1;
        if(pageIndex !=null){
            currentPage=Integer.parseInt(pageIndex);
        }
        int pageSize= Constants.pageSize;

//获取总数量
        int smbmsBillCount = 0;
        try {
            smbmsBillCount = smbmsBillService.getproviderCount(productname,ispayment);
//获取总页数
            PageSupport pageSupport=new PageSupport();
//当前页
            pageSupport.setCurrentPageNo(currentPage);
//页面容量
            pageSupport.setPageSize(pageSize);
//总数量
            pageSupport.setTotalCount(smbmsBillCount);
//获取总页数
            int totalPageCount=pageSupport.getTotalPageCount();
//控制首页和尾页
            if(currentPage<1){
                currentPage=1;
            }else if(currentPage > totalPageCount){
                currentPage=totalPageCount;
            }
            List<SmbmsBill> smbmsBills = smbmsBillService.queryBillByList(productname,ispayment,currentPage,pageSize,providerId);
            if(smbmsBills.size()>0){
//把数据放在request作用域中
                req.setAttribute("pages",pageSupport);
                req.setAttribute("productname",productname);
                req.setAttribute("list",smbmsBills);
                //把是否付款放在request作用域中
                req.setAttribute("ispayment",ispayment);
                //把供应商名单放在request作用域中
                req.setAttribute("tigong",providerId);
            }
            //供应商选择数据（待编写）
            List<SmbmsProvider> smbmsProviders = smbmsProviderService.queryPronName();
            if(smbmsProviders.size()>0){
//把角色存在request域中
                req.setAttribute("proNameList",smbmsProviders);
            }
//转发
            req.getRequestDispatcher("BillList.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
